package com.example;

/**
 * @desc:
 * @author: Shoko
 * @date: 2025/6/1
 */
// ChatClient.java
import java.io.*;
import java.net.*;

public class ChatClient {
    public static void main(String[] args) {
        try {
            Socket socket = new Socket("localhost", 12345);
            PrintWriter out = new PrintWriter(socket.getOutputStream(), true);
            BufferedReader in = new BufferedReader(new InputStreamReader(socket.getInputStream()));
            BufferedReader stdIn = new BufferedReader(new InputStreamReader(System.in));

            System.out.println("请输入会员类型和名称 (diamond/common,name):");
            String userInput = stdIn.readLine();
            out.println(userInput);

            // 接收消息线程
            new Thread(() -> {
                try {
                    String serverResponse;
                    while ((serverResponse = in.readLine()) != null) {
                        System.out.println(serverResponse);
                    }
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }).start();

            // 发送消息
            while (true) {
                System.out.println("请输入消息格式 (text/image:接收者:内容):");
                userInput = stdIn.readLine();
                if (userInput.equals("exit")) break;
                out.println(userInput);
            }

            socket.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}